package com.hnucm.c202001020216.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.gyf.immersionbar.ImmersionBar;
import com.hnucm.c202001020216.DataApplication;
import com.hnucm.c202001020216.R;
import com.hnucm.c202001020216.api.BrandApi;
import com.hnucm.c202001020216.databinding.CaritemBinding;
import com.hnucm.c202001020216.pojo.Brand;
import com.hnucm.c202001020216.utils.RetrofitUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author starr
 */
public class CarBrandActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    MyAdapter myAdapter;
    ImageView returnBtn;
    public List<Brand.PageDTO.ListDTO> brandList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_brand);

        recyclerView=findViewById(R.id.recyclerView2);

        returnBtn=findViewById(R.id.imageView7);
        returnBtn.setOnClickListener(
                (view)->{
                    finish();
                }
        );

        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .init();
/*************************************************************************************************************************/
        //1.请求数据
        BrandApi api= RetrofitUtils.getRetrofit(DataApplication.HttpURL).create(BrandApi.class);
        Call<Brand> call= api.getAllBrand("奥迪");

        call.enqueue(new Callback<Brand>() {
            @Override
            public void onResponse(Call<Brand> call, Response<Brand> response) {
                Brand brand=response.body();
                brandList.addAll(brand.page.list);

                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<Brand> call, Throwable t) {

            }
        });

        myAdapter=new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }



    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            CaritemBinding caritemBinding= DataBindingUtil.inflate(LayoutInflater.from(CarBrandActivity.this),R.layout.caritem,
                    parent,false);
            MyViewHolder myViewHolder=new MyViewHolder(caritemBinding.getRoot());
            myViewHolder.caritemBinding=caritemBinding;
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.caritemBinding.setCarItem(brandList.get(position));
        }

        @Override
        public int getItemCount() {
            return brandList.size();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{

        CaritemBinding caritemBinding;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}